package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.CancellationPolicyLogic;

import data.CancellationPolicy;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class CancellationPolicyService extends HttpServlet{
	//http://localhost:8080/cancellationpolicyService?func=addCancellationPolicy&title=1&Description=q&Percentcharge=true&fix=true&chargeDay=true&value=2&beforeCheckIn=2&charge=30&isDay=true
	//http://localhost:8080/cancellationpolicyService?func=editCancellationPolicy&cancellationpolicyId=876&title=122222222222222&Description=q&Percentcharge=true&fix=true&chargeDay=true&value=2&beforeCheckIn=2&charge=30&isDay=true
	//http://localhost:8080/cancellationpolicyService?func=delCancellationPolicy&cancellationpolicyId=878
	//http://localhost:8080/cancellationpolicyService?func=changeStatus&cancellationpolicyId=878
	//http://localhost:8080/cancellationpolicyService?func=getCancellationPolicy
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addCancellationPolicy")){
				AddCancellationPolicy(req,resp);
			}else if(doClass.equals("editCancellationPolicy")){
				EditCancellationPolicy(req,resp);
			}else if(doClass.equals("delCancellationPolicy")){
				DeleteCancellationPolicy(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getCancellationPolicy")){
				GetCancellationPolicy(req,resp);
			}else if(doClass.equals("getAllCancellationPolicy4Admin")){
				GetAllCancellationPolicy4Admin(req,resp);
			}else if(doClass.equals("getCancellationPolicyById")){
				GetCancellationPolicyById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddCancellationPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		int beforeAmount = Integer.parseInt(req.getParameter("beforeAmount"));
		boolean isDay = Boolean.parseBoolean(req.getParameter("isDay"));
		int Day = 0;
		int Hours = 0;
		if(isDay){
			Day = beforeAmount;
		}else{
			Hours = beforeAmount;
		}
		String Description = req.getParameter("description");
		boolean isPercent = Boolean.parseBoolean(req.getParameter("isPercent"));
		boolean isFix = Boolean.parseBoolean(req.getParameter("isFix"));
		boolean isRoomNight = Boolean.parseBoolean(req.getParameter("isRoomNight"));
		double value = Double.parseDouble(req.getParameter("chargeAmount"));
		
		new CancellationPolicyLogic().AddCancellationPolicy(hotelId,title,Description,isPercent,isFix,isRoomNight,value,Day,Hours);
		
		GetAllCancellationPolicy4Admin(req, resp);

	}

	private void EditCancellationPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long cancellationpolicyId = Long.parseLong(req.getParameter("cancellationpolicyId"));
		String title = req.getParameter("title");
		int beforeAmount = Integer.parseInt(req.getParameter("beforeAmount"));
		boolean isDay = Boolean.parseBoolean(req.getParameter("isDay"));
		int Day = 0;
		int Hours = 0;
		if(isDay){
			Day = beforeAmount;
		}else{
			Hours = beforeAmount;
		}
		String Description = req.getParameter("description");
		boolean isPercent = Boolean.parseBoolean(req.getParameter("isPercent"));
		boolean isFix = Boolean.parseBoolean(req.getParameter("isFix"));
		boolean isRoomNight = Boolean.parseBoolean(req.getParameter("isRoomNight"));
		double value = Double.parseDouble(req.getParameter("chargeAmount"));
				
		new CancellationPolicyLogic().EditCancellationPolicy(cancellationpolicyId,title,Description,isPercent,isFix,isRoomNight,value,Day,Hours);

		GetAllCancellationPolicy4Admin(req, resp);

	}

	private void DeleteCancellationPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		
		for(int i = 0; i < id.split(",").length ; i++){
			new CancellationPolicyLogic().DeleteCancellationPolicy(Long.parseLong(id.split(",")[i]));
		}
		
		GetAllCancellationPolicy4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long cancellationpolicyId = Long.parseLong(req.getParameter("cancellationpolicyId"));

		Object obj = new CancellationPolicyLogic().ChangeStatus(cancellationpolicyId);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetCancellationPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		Object obj = new CancellationPolicyLogic().GetAllCancellationPolicy(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllCancellationPolicy4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long line = Long.parseLong(req.getParameter("line"));
		long offset = Long.parseLong(req.getParameter("offset"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		List<CancellationPolicy> obj = new CancellationPolicyLogic().GetAllCancellationPolicy4AdminLimit(hotelId,offset,line);
		double dataSize = new CancellationPolicyLogic().GetAllCancellationPolicy(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetCancellationPolicyById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		CancellationPolicy c = new CancellationPolicyLogic().GetCancellationPolicy(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
}
